#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
using namespace std;

int main()
{
    // 读取二进制文件
    ifstream ifs("test.img", ios::in | ios::binary);
    if(ifs.is_open() == false)
    {
        cout << "open read file failed!" << endl;
        return -1;
    }

    // 写入二进制文件
    ofstream ofs("test4.img", ios::out | ios::binary);
    if(ofs.is_open() == false)
    {
        cout << "open write file failed!" << endl;
        return -1;
    }

    uint8_t buf[4096];
    int len;
    while(1)
    {
        memset(buf, 0, sizeof(buf));
        ifs.read((char*)buf, sizeof(buf));
        len = ifs.gcount();     // 返回读取的字节数
        if(len == 0)
        {
            printf("输入任意按键继续\n");
            fgets((char *)buf, sizeof(buf), stdin);
            break;
        }
        // printf("len = %d\n", len);

        ofs.write((char*)buf, len);
    }

    ofs.close();
    ifs.close();

    return 0;
}
